﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LIM.model;
using LIM.controller;
using Telerik.WinControls.UI;

using p_Library;

namespace LIM.forms.uc_SYS_QuanTriNguoiDung
{
    public partial class uc_SYS_QuanTriNguoiDung_NhomNguoiDung : BaseUserControl
    {        
        //DanhMucPhongBanController _DanhMucPhongBanController = new DanhMucPhongBanController();

        radForm_SYS_QuanTriNguoiDung parentForm=null;
        public uc_SYS_QuanTriNguoiDung_NhomNguoiDung()
        {
            InitializeComponent();

            // Khai báo các controls sẽ áp dụng phân quyền trong hệ thống
            ControlPermissionList.Add(this.radButton_BoQuyen);
            ControlPermissionList.Add(this.radButton_GanQuyen);
            // Kết thúc khai báo
        }

        void parentForm_OnRowChange()
        {
            if (parentForm.SelectedEntity != null)
            {
                BindControls();
            }            
        }

        private void uc_SYS_QuanTriNguoiDung_ThongTinNhanVien_Load(object sender, EventArgs e)
        {
            parentForm = (radForm_SYS_QuanTriNguoiDung)this.ParentForm;
            if (parentForm != null)
            {
                parentForm.OnRowChange += new radForm_SYS_QuanTriNguoiDung.GridViewDoubleClick(parentForm_OnRowChange);
            }
            if (parentForm.SelectedEntity != null)
            {
                BindControls();
            }            
        }

        protected void BindControls()
        {
            this.radListControl_ChuaGan.DataSource = parentForm._SYSController.GetList_UnassignedRoles(parentForm.SelectedEntity.RowID);
            this.radListControl_DaGan.DataSource = parentForm._SYSController.GetList_AssignedRoles(parentForm.SelectedEntity.RowID);
        }
                
        private void radButton_BoQuyen_Click(object sender, EventArgs e)
        {
            tbl_SYS_Roles role = (tbl_SYS_Roles)this.radListControl_DaGan.SelectedItem.DataBoundItem;
            parentForm._SYSController.Remove_UserInRole(parentForm.SelectedEntity.tbl_SYS_Users, role);
            BindControls();
        }

        private void radButton_GanQuyen_Click(object sender, EventArgs e)
        {
            tbl_SYS_Roles role=(tbl_SYS_Roles)this.radListControl_ChuaGan.SelectedItem.DataBoundItem;
            parentForm._SYSController.Add_UserInRole(parentForm.SelectedEntity.tbl_SYS_Users, role);
            BindControls();
        }

    }
}
